package com.ruoyi.web.controller.cat;


import com.ruoyi.cat.domain.CatArticletaskTable;
import com.ruoyi.cat.domain.DTO.AdvertTimeDTO;
import com.ruoyi.cat.domain.DTO.ArticletaskDTO;
import com.ruoyi.cat.service.ICatArticletaskTableService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理端-外链相关接口")
@RestController
@RequestMapping("/system/outside")
public class OutsideController extends BaseController {

    @Autowired
    private ICatArticletaskTableService catArticletaskTableService;


    /**
     * 查询文章任务列表
     */
    //@PreAuthorize("@ss.hasPermi('system:table:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatArticletaskTable catArticletaskTable)
    {
        startPage();
        catArticletaskTable.setArticleMode("1");
        List<CatArticletaskTable> list = catArticletaskTableService.selectCatArticletaskTableList(catArticletaskTable);
        return getDataTable(list);
    }


    /**
     * 新增文章任务
     */
    @PostMapping
    public AjaxResult add(@RequestBody CatArticletaskTable catArticletaskTable)
    {
        catArticletaskTable.setArticleMode("1");
        return toAjax(catArticletaskTableService.insertCatArticletaskTableOutside(catArticletaskTable));
    }

    @GetMapping(value = "updateBody/{id}")
    public AjaxResult updateBody(@PathVariable("id") Long id)
    {
        return success(catArticletaskTableService.updateBody(id));
    }

    /**
     * 获取文章任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:table:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(catArticletaskTableService.selectCatArticletaskContentTableById(id));
    }


    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(catArticletaskTableService.deleteCatArticletaskTableByIds(ids));
    }

    @PostMapping("updateAdvertTime")
    public AjaxResult updateAdvertTime(@RequestBody CatArticletaskTable catArticletaskTable)
    {
        return toAjax(catArticletaskTableService.updateAdvertTime(catArticletaskTable));
    }

    @PostMapping("getAdvertTime/{id}")
    public AjaxResult getAdvertTime(@PathVariable("id") Long id)
    {
        AdvertTimeDTO advertTimeDTO = new AdvertTimeDTO();
        ArticletaskDTO articletaskDTO = catArticletaskTableService.selectCatArticletaskById(id);
        advertTimeDTO.setId(advertTimeDTO.getId());
        if (articletaskDTO != null){
            if (articletaskDTO.getAdvertTime() != null){
                advertTimeDTO.setAdvertTime(articletaskDTO.getAdvertTime());
            }
        }
        return success(advertTimeDTO);
    }

    @PostMapping("offShelf")
    public AjaxResult offShelf(@RequestBody CatArticletaskTable catArticletaskTable)
    {
        if (StringUtils.isNotEmpty(catArticletaskTable.getArticleState())){
            //下载
            catArticletaskTable.setActivityState("2");
        }
        return toAjax(catArticletaskTableService.updateCatArticletaskTable(catArticletaskTable));
    }

    @PostMapping("updateVisitors")
    public AjaxResult updateVisitors(@RequestBody CatArticletaskTable catArticletaskTable)
    {
        return toAjax(catArticletaskTableService.updateVisitors(catArticletaskTable));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody CatArticletaskTable catArticletaskTable)
    {
        return toAjax(catArticletaskTableService.updateCatArticletaskTable(catArticletaskTable));
    }
}
